package com.ruoyi.management.basics.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.management.basics.domain.TEmployeeMer;
import com.ruoyi.management.basics.ro.DownloadTemplateParam;
import com.ruoyi.management.basics.ro.GetEmployeeMerListParam;
import com.ruoyi.management.basics.ro.GetEmployeesParam;
import com.ruoyi.management.basics.ro.ImportTemplateParam;
import com.ruoyi.management.basics.service.ITEmployeeMerService;
import com.ruoyi.management.basics.service.ITEmployeeService;
import com.ruoyi.management.basics.vo.DownloadTemplateVo;
import com.ruoyi.management.basics.vo.DriverMerListVo;
import com.ruoyi.management.basics.vo.GetEmployeesVo;
import com.ruoyi.management.carservicestatics.vo.GetCarServiceStaticsListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * 驾驶员商户信息Controller
 * 
 * @author ruoyi
 * @date 2021-12-07
 */
@RestController
@Api(tags = "驾驶员商户信息管理")
@RequestMapping("/employee/mer")
public class TEmployeeMerController extends BaseController
{
    @Autowired
    private ITEmployeeMerService tEmployeeMerService;
    @Autowired
    private ITEmployeeService employeeService;

    /**
     * 查询驾驶员商户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:mer:list')")
    @GetMapping("/list")
    @ApiOperation("询驾驶员商户信息列表")
    public TableDataInfo list(GetEmployeeMerListParam tEmployeeMer)
    {
        startPage();
        List<DriverMerListVo> list = tEmployeeMerService.selectTEmployeeMerVoList(tEmployeeMer);
        return getDataTable(list);
    }

    /**
     * 导出驾驶员商户信息列表
     */
    @Log(title = "驾驶员商户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出驾驶员商户信息列表")
    public AjaxResult export(TEmployeeMer tEmployeeMer)
    {
        List<TEmployeeMer> list = tEmployeeMerService.selectTEmployeeMerList(tEmployeeMer);
        ExcelUtil<TEmployeeMer> util = new ExcelUtil<TEmployeeMer>(TEmployeeMer.class);
        return util.exportExcel(list, "驾驶员商户信息数据");
    }

    /**
     * 获取驾驶员商户信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:mer:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取驾驶员商户信息详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tEmployeeMerService.selectTEmployeeMerVoById(id));
    }

    /**
     * 新增驾驶员商户信息
     */
    @PostMapping
    @ApiOperation("新增驾驶员商户信息")
    public AjaxResult add(@RequestBody TEmployeeMer tEmployeeMer)
    {
        return toAjax(tEmployeeMerService.insertTEmployeeMer(tEmployeeMer));
    }

    /**
     * 修改驾驶员商户信息
     */
    //@PreAuthorize("@ss.hasPermi('system:mer:edit')")
    @Log(title = "驾驶员商户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改驾驶员商户信息")
    public AjaxResult edit(@Valid @RequestBody TEmployeeMer tEmployeeMer)
    {
        return toAjax(tEmployeeMerService.updateTEmployeeMer(tEmployeeMer));
    }

    /**
     * 删除驾驶员商户信息
     */
    //@PreAuthorize("@ss.hasPermi('system:mer:remove')")
    @Log(title = "驾驶员商户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除驾驶员商户信息")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tEmployeeMerService.deleteTEmployeeMerByIds(ids));
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/download-template")
    @ApiOperation("下载导入模板（按驾驶员导入）")
    public AjaxResult downloadTemplate(@RequestBody @Valid DownloadTemplateParam param)
    {
        List<DownloadTemplateVo> list = tEmployeeMerService.downloadTemplate(param);
        ExcelUtil<DownloadTemplateVo> util = new ExcelUtil<DownloadTemplateVo>(DownloadTemplateVo.class);
        return util.exportExcel(list, "驾驶员商户信息模板");
    }

    @GetMapping("/import-template")
    @ApiOperation("下载导入模板（按企业导入）")
    public AjaxResult ImportTemplate(@Valid ImportTemplateParam param)
    {
        List<DownloadTemplateVo> list = tEmployeeMerService.ImportTemplate(param);
        ExcelUtil<DownloadTemplateVo> util = new ExcelUtil<DownloadTemplateVo>(DownloadTemplateVo.class);
        return util.exportExcel(list, "驾驶员商户信息模板");
    }


    @PostMapping("/import-data")
    @ApiOperation("导入")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        if(file == null){
            return AjaxResult.error("导入文件不能为空！");
        }
        ExcelUtil<DownloadTemplateVo> util = new ExcelUtil<DownloadTemplateVo>(DownloadTemplateVo.class);
        List<DownloadTemplateVo> paramList = util.importExcel(file.getInputStream());
        return toAjax(tEmployeeMerService.importData(paramList));
    }

    @GetMapping("/select-drivers")
    @ApiOperation("查询企业下的驾驶员")
    public TableDataInfo selectDrivers(@Valid GetEmployeesParam param)
    {
        startPage();
        List<GetEmployeesVo> list = employeeService.getDrivers(param);
        return getDataTable(list);
    }
}
